﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mal.balades.Business.Web;
using mal.balades.Business.Log;

namespace mal.balades.Web
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SiteMapProvider headerProvider = SiteMap.Providers["Header"];
                SiteMapProvider footerProvider = SiteMap.Providers["Footer"];
                SiteMapNode root = headerProvider.RootNode;
                SiteMapNode currentNode = headerProvider.CurrentNode;

                List<LocalSiteMapNode> nodes = new List<LocalSiteMapNode>();

                foreach (SiteMapNode item in root.ChildNodes)
                {
                    LocalSiteMapNode node = new LocalSiteMapNode(item);
                    if (item == currentNode)
                        node.IsSelected = true;
                    else if (currentNode == null)
                    {
                        if (Request.ServerVariables["SCRIPT_NAME"] == "/default.aspx")
                        {
                            if (item.Url == "/")
                                node.IsSelected = true;
                        }
                    }
                    nodes.Add(node);

                }

                headers.DataSource = nodes;
                headers.DataBind();

                footers.DataSource = footerProvider.RootNode.ChildNodes;
                footers.DataBind();


                if (AudienceTrackingHelper.Instance.TrackingEnabled)
                    AudienceTraching.Visible = true;
                else
                    AudienceTraching.Visible = false;

            }
            catch (Exception exp)
            {
                MalLogManager.WebLogger.Error(string.Format("SiteMaster Error : {0}", exp.Message), exp);
            }
        }

        private class LocalSiteMapNode
        {
            private SiteMapNode node;

            public LocalSiteMapNode(SiteMapNode node)
            {
                this.node = node;
                this.IsSelected = false;
            }

            public string Url
            {
                get
                {
                    return this.node.Url;
                }
            }

            public string Title
            {
                get
                {
                    return this.node.Title;
                }
            }

            public string Description
            {
                get
                {
                    return this.node.Description;
                }
            }

            public string CssClass
            {
                get
                {
                    if (IsSelected)
                        return "headerOn";
                    else
                        return "headerOff";
                }
            }

            public bool IsSelected { get; set; }
        }
    }
}
